import axios from 'axios'
import { Message } from 'element-ui'
import store from '@/store'
// import { getToken } from '@/utils/auth'
import qs from 'qs'
// console.log(store)
const service = axios.create({
  timeout: 60000
})

service.interceptors.request.use(
  config => {
    if (store.getters.getToken) {  
      config.headers['Authorization'] = store.getters.getToken
      // config.headers['Access-Control-Allow-Origin'] = '*'
      // console.log(config.headers['Authorization'])
    }
    // console.log(config);
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

service.interceptors.response.use(
  response => {
    const res = response

    if (res.status !== 200) {
      Message({
        message: 'Error',
        type: 'error',
        duration: 5 * 1000
      })
      return Promise.reject(res)
    } else {
      // console.log(res)
      return res.data
    }
  },
  error => {
    return Promise.reject(error)
  }
)
export const upload = (url, params) => {
  return service.post(url, params, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

export default service
